package requests

import (
	"github.com/gin-gonic/gin"
	"github.com/saint-yellow/go-pl/toys/gohub/app/requests/validators"
	"github.com/saint-yellow/go-pl/toys/gohub/pkg/captcha"
	"github.com/thedevsaddam/govalidator"
)

type VeifyCodePhoneRequest struct {
    CaptchaID     string `json:"captcha_id,omitempty" valid:"captcha_id"`
    CaptchaAnswer string `json:"captcha_answer,omitempty" valid:"captcha_answer"`

    Phone string `json:"phone,omitempty" valid:"phone"`
}

// VeifyCodePhone 验证表单，返回长度等于零即通过
func VeifyCodePhone(data interface{}, c *gin.Context) map[string][]string {

    // 1. 定制认证规则
    rules := govalidator.MapData{
        "phone":          []string{"required", "digits:11"},
        "captcha_id":     []string{"required"},
        "captcha_answer": []string{"required", "digits:6"},
    }

    // 2. 定制错误消息
    messages := govalidator.MapData{
        "phone": []string{
            "required:手机号为必填项，参数名称 phone",
            "digits:手机号长度必须为 11 位的数字",
        },
        "captcha_id": []string{
            "required:图片验证码的 ID 为必填",
        },
        "captcha_answer": []string{
            "required:图片验证码答案必填",
            "digits:图片验证码长度必须为 6 位的数字",
        },
    }

    errs := validate(data, rules, messages)

    // 图片验证码
    _data := data.(*VeifyCodePhoneRequest)
    if ok := captcha.NewCaptcha().VerifyCaptcha(_data.CaptchaID, _data.CaptchaAnswer); !ok {
        errs["captcha_answer"] = append(errs["captcha_answer"], "图片验证码错误")
    }

    return errs
}

type VeifyCodeEmailRequest struct {
    CaptchaID     string `json:"captcha_id,omitempty" valid:"captcha_id"`
    CaptchaAnswer string `json:"captcha_answer,omitempty" valid:"captcha_answer"`

    Email string `json:"email,omitempty" valid:"email"`
}

// VeifyCodeEmail 验证表单，返回长度等于零即通过
func VeifyCodeEmail(data interface{}, c *gin.Context) map[string][]string {

    // 1. 定制认证规则
    rules := govalidator.MapData{
        "email":          []string{"required", "min:4", "max:30", "email"},
        "captcha_id":     []string{"required"},
        "captcha_answer": []string{"required", "digits:6"},
    }

    // 2. 定制错误消息
    messages := govalidator.MapData{
        "email": []string{
            "required:Email 为必填项",
            "min:Email 长度需大于 4",
            "max:Email 长度需小于 30",
            "email:Email 格式不正确，请提供有效的邮箱地址",
        },
        "captcha_id": []string{
            "required:图片验证码的 ID 为必填",
        },
        "captcha_answer": []string{
            "required:图片验证码答案必填",
            "digits:图片验证码长度必须为 6 位的数字",
        },
    }

    errs := validate(data, rules, messages)

    // 图片验证码
    _data := data.(*VeifyCodeEmailRequest)
    errs = validators.ValidateCaptcha(_data.CaptchaID, _data.CaptchaAnswer, errs)

    return errs
}
